package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.pojo.ProductionWorkOrders;
import com.intelligent.service.ProductionWorkOrdersService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/productionWorkOrders")
public class ProductionWorkOrdersController {

	@Resource
	private ProductionWorkOrdersService productionWorkOrdersService;

	@GetMapping("/page")
	public IPage<Map<String, Object>> page(
			@RequestParam(defaultValue = "1") long current,
			@RequestParam(defaultValue = "10") long size,
			@RequestParam(required = false) String workOrderNo,
			@RequestParam(required = false) Integer status,
			@RequestParam(required = false) String productCode,
			@RequestParam(required = false) String productName,
			@RequestParam(required = false) Integer overtimeStatus,
			@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date plannedStartBegin,
			@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date plannedStartEnd,
			@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date plannedCompletionBegin,
			@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date plannedCompletionEnd,
			@RequestParam(required = false) String username
	) {
		Page<?> page = new Page<>(current, size);
		return productionWorkOrdersService.pageQuery(page, workOrderNo, status, productCode, productName, overtimeStatus,
				plannedStartBegin, plannedStartEnd, plannedCompletionBegin, plannedCompletionEnd,username);
	}

	/**
	 * 添加生产工单
	 * @param productionWorkOrders 生产工单对象
	 * @return 添加结果
	 */
	@PostMapping("/add")
	public Map<String, Object> addProductionWorkOrder(@RequestBody ProductionWorkOrders productionWorkOrders) {
		Map<String, Object> result = new java.util.HashMap<>();
		try {
			boolean success = productionWorkOrdersService.addProductionWorkOrder(productionWorkOrders);
			if (success) {
				result.put("code", 200);
				result.put("message", "添加成功");
				result.put("data", productionWorkOrders);
			} else {
				result.put("code", 500);
				result.put("message", "添加失败");
			}
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "添加失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 修改生产工单
	 * @param productionWorkOrders 生产工单对象
	 * @return 修改结果
	 */
	@PutMapping("/update")
	public Map<String, Object> updateProductionWorkOrder(@RequestBody ProductionWorkOrders productionWorkOrders) {
		Map<String, Object> result = new java.util.HashMap<>();
		try {
			boolean success = productionWorkOrdersService.updateProductionWorkOrder(productionWorkOrders);
			if (success) {
				result.put("code", 200);
				result.put("message", "修改成功");
				result.put("data", productionWorkOrders);
			} else {
				result.put("code", 500);
				result.put("message", "修改失败");
			}
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "修改失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 删除生产工单
	 * @param id 工单ID
	 * @return 删除结果
	 */
	@DeleteMapping("/delete/{id}")
	public Map<String, Object> deleteProductionWorkOrder(@PathVariable("id") Long id) {
		Map<String, Object> result = new java.util.HashMap<>();
		try {
			boolean success = productionWorkOrdersService.deleteProductionWorkOrderById(id);
			if (success) {
				result.put("code", 200);
				result.put("message", "删除成功");
			} else {
				result.put("code", 500);
				result.put("message", "删除失败");
			}
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "删除失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 根据ID查询生产工单详情
	 * @param id 工单ID
	 * @return 详情数据
	 */
	@GetMapping("/detail/{id}")
	public Map<String, Object> getDetailsById(@PathVariable("id") Long id) {
		Map<String, Object> result = new java.util.HashMap<>();
		try {
			Map<String, Object> data = productionWorkOrdersService.getDetailsById(id);
			if (data != null) {
				result.put("code", 200);
				result.put("message", "查询成功");
				result.put("data", data);
			} else {
				result.put("code", 404);
				result.put("message", "未找到记录");
			}
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "查询失败：" + e.getMessage());
		}
		return result;
	}
}
